#ifndef YXNET_H
#define YXNET_H

#if defined(_WIN32) || defined(_WIN64)
#include <winsock2.h>
#elif defined(__linux__)
#include <sys/socket.h>
#include <arpa/inet.h>
#include <poll.h>
#else
#error "not support platform"
#endif

#include <stdlib.h>

#define YX_NET_NONE     0
#define YX_NET_READABLE 1
#define YX_NET_WRITABLE 2

typedef struct EventLoop EventLoop;

typedef void file_proc(EventLoop *event_loop, int fd, int mask, void *clientData);

typedef struct {
    int        mask;
    file_proc *read_proc;
    file_proc *write_proc;
    void      *client_data;
} FileEvent;

typedef struct {
    int fd;
    int mask;
} FiredEvent;

typedef struct EventLoop {
    // int max_fd; /* 目前已注册的最大文件描述符 */
    int         set_size;     /* 目前已追踪的文件描述符数量 */
    FileEvent  *events;       /* 已注册的文件事件 */
    FiredEvent *fired_events; /* 已就绪的文件事件 */
    int         stop;         // 事件处理器的开关
    void       *api_data;     /* 存储不同的多路复用库的state */
} EventLoop;


int  yxApiCreate(EventLoop *event_loop);
void yxApiDelete(EventLoop *event_loop);
int  yxApiAddEvent(EventLoop *event_loop, int fd, int mask);
void yxApiDelEvent(EventLoop *event_loop, int fd, int delmask);
int  yxApiPoll(EventLoop *event_loop, long long timeout);

EventLoop *net_create_event_loop(int set_size);
void       net_delete_event_loop(EventLoop *event_loop);
int        net_create_file_event(EventLoop *event_loop, int fd, int mask, file_proc *proc, void *clientData);
void       net_delete_file_event(EventLoop *event_loop, int fd, int mask);
int        net_create_server_socket(char *host, int port);
int        net_set_non_block(int fd);
int        net_tcp_non_block_connect(char *host, int port);
int        net_wait(int fd, int mask, long long timeout);

#endif //YXNET_H
